// 文件为Movie Retal的初始文件
// Created by Administrator on 2021/2/27.
//

#include "MovieRental.h"

void Customer::UpdateCustomerData() {
    this->customerData.name = GetName();
    this->customerData.rental.clear();

    double amount = 0;
    int frequentRenterPoints = 0;
    for (auto &each : _rentals) {
        RentalData data;
        data.amount = each.GetMovie().GetAmount(each.GetDaysRented());
        data.title = each.GetMovie().GetTitle();
        amount += data.amount;
        frequentRenterPoints += each.GetMovie().GetPoints(each.GetDaysRented());
        customerData.rental.push_back(data);
    }

    this->customerData.frequentRenterPoints = frequentRenterPoints;
    this->customerData.totalAmount = amount;
}

std::string Customer::statement() {
    std::string output;
    output += ("Rental Record for " + this->customerData.name + "\n");
    for (auto &each : this->customerData.rental) {
        char ch[64] = {0};
        sprintf_s(ch, "%g", each.amount);
        output += ("\t" + each.title + "\t" + std::string(ch) + "\n");
    }

    char amount[64] = {0};
    sprintf_s(amount, "%g", this->customerData.totalAmount);
    output += ("Amount owed is " + std::string(amount) + "\n");

    char points[64] = {0};
    sprintf_s(points, "%d", this->customerData.frequentRenterPoints);
    output += ("You earned " + std::string(points) + " frequent renter points");

    return output;
}

std::string Customer::GetOutputHtml() {
    std::string output;
    output += ("<h1>Rental Record for <em>" + this->customerData.name +
               "</em></h1>\n");
    output += "<table>\n";
    for (auto &each : this->customerData.rental) {
        output += "\t<tr>";
        char ch[64] = {0};
        sprintf_s(ch, "%g", each.amount);
        output +=
            ("<td>" + each.title + "</td><td>" + std::string(ch) + "</td>");
        output += "</tr>\n";
    }
    output += "</table>\n";

    char amount[64] = {0};
    sprintf_s(amount, "%g", this->customerData.totalAmount);
    output += ("<p>Amount owed is <em>" + std::string(amount) + "</em></p>\n");

    char points[64] = {0};
    sprintf_s(points, "%d", this->customerData.frequentRenterPoints);
    output += ("<p>You earned <em>" + std::string(points) +
               "</em> frequent renter points</p>");

    return output;
}